package com.jason.service.impl;

import com.jason.base.BaseServiceImpl;
import com.jason.core.util.ReflectionUtil;
import com.jason.entity.SysMenu;
import com.jason.entity.SysPermission;
import com.jason.mapper.SysPermissionMapper;
import com.jason.service.ISysPermissionService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sj
 * @since 2018-04-25
 */
@Service
public class SysPermissionServiceImpl extends BaseServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {

    @Override
    public Map<String, Object> selectMenusByUser(Long userId) {
        List<SysMenu> menus = baseMapper.selectMenusByUser(userId);
        List<SysMenu> menuTreeList = buildTree(menus, "pid", 0L);
        //间隔list转换为linkedHashMap
        return menuTreeList.stream().collect(Collectors.toMap(SysMenu::getCode,
                Function.identity(), (key1, key2) -> key2, LinkedHashMap::new));
    }


    private List<SysMenu> buildTree(List<SysMenu> list, String treeKey, Long treeRoot) {
        List<SysMenu> treeList = new ArrayList();
        Iterator<SysMenu> iterator = list.iterator();
        while(iterator.hasNext()) {
            SysMenu t = iterator.next();
            Long keyValue = (Long)ReflectionUtil.getFieldValue(t, treeKey);
            if (keyValue.equals(treeRoot)) {
                treeList.add(t);
                iterator.remove();
            }
        }
        treeList.sort(comparing(SysMenu::getSort));
        Vector<Thread> threads = new Vector();
        Iterator<SysMenu> iterator2 =treeList.iterator();
        while(iterator2.hasNext()) {
            SysMenu root = iterator2.next();
            Thread iThread = new Thread(new Runnable() {
                public void run() {
                    buildChild(list, treeKey, root);
                }
            });
            threads.add(iThread);
            iThread.start();
        }
        Iterator<Thread> iterator3 = threads.iterator();
        while(iterator3.hasNext()) {
            Thread iThread = iterator3.next();

            try {
                iThread.join();
            } catch (InterruptedException var9) {
                var9.printStackTrace();
            }
        }
        return treeList;
    }

    private void buildChild(List<SysMenu> list, String treeKey, SysMenu root) {
        Long rootId = root.getId();
        List<SysMenu> childList = new ArrayList();
        Iterator<SysMenu> iterator = list.iterator();

        while(iterator.hasNext()) {
            SysMenu sysPermission = iterator.next();
            Long keyValue = (Long)ReflectionUtil.getFieldValue(sysPermission, treeKey);
            if (keyValue.equals(rootId)) {
                buildChild(list, treeKey, sysPermission);
                childList.add(sysPermission);
            }
        }
        childList.sort(comparing(SysMenu::getSort));
        root.setChildren(childList);
    }
}
